#include <iostream>
#include<functional>
using namespace std;


class Date
{
public:
    Date(int n,int y,int r)
    :_n(n)
    ,_y(y)
    ,_r(r)
    {}
    ~Date()
    {
        cout << "~Date" << endl;
    }
private:
    int _n;
    int _y;
    int _r;
};


namespace bit
{
    template<class T>
    class shared_ptr
    {
    public:
        //构造
        shared_ptr(T* ptr = nullptr)
           :_ptr(ptr)
           ,_pco(new int(1))
        {
        }
        //删除的构造
        template<class D>
        shared_ptr(T* ptr,D del)
        :_ptr(ptr)
        ,_pco(new int(1))
        ,_del(del)
        {}
        //拷贝构造
        shared_ptr(const shared_ptr<T>& shar)
        :_ptr(shar._ptr)
        ,_pco(shar._pco)
        ,_del(shar._del)
        {
            ++(*_pco);
        }

        void rese() {
            if (--(*_pco) == 0) {
                _del(_ptr);
                delete _pco;
                _ptr = nullptr;
                _pco = nullptr;

            }
        }
        ~shared_ptr()
        {
            rese();
        }


        shared_ptr<T>& operator=(const shared_ptr<T>& date)
        {
            if(_ptr != date._ptr)
            {
                rese();
                _ptr = date._ptr;
                _pco = date._pco;
                _del = date._del;
            }
        }

        T& operator*()
        {
            return *_ptr;
        }
        T* operator->()
        {
            return _ptr;
        }
    private:
        T* _ptr;
        int* _pco;//计数
        function<void(T*)> _del = [](T* ptr){delete ptr;};
    };

    template<class T>
    class auto_ptr
    {
    public:
        auto_ptr(T* ptr)
        :_ptr(ptr)
        {
        }
        auto_ptr(const auto_ptr& au)
        :_ptr(au._ptr)
        {
            au._ptr = nullptr;
        }

    private:
        T* _ptr;
    };


}

int main()
{
    bit::shared_ptr<Date> sp1(new Date(2999,2,2));
    bit::shared_ptr<Date> sp2(sp1);
    bit::shared_ptr<Date> sp3(new Date(2222,1,3));
    return 0;
}
